﻿#region

using System;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using BusinessObjects;
using DataAccessLayer;

#endregion

namespace BusinessLogicLayer
{
    public static class UserBLL
    {
        public static bool IsUsernameTaken(string uname)
        {
            return UserDAL.IsUsernameTaken(uname);
        }

        public static bool LoginUser(User user)
        {
            user.Password = EncryptPassword(user.Password);
            return UserDAL.LoginUser(user);
        }

        public static bool IsBannedUser(User user)
        {
            return UserDAL.IsBannedUser(user);
        }

        public static void UpdatePassword(User user)
        {
            user.Password = EncryptPassword(user.Password);
            UserDAL.UpdatePassword(user);
        }

        public static bool IsBannedUser(string username)
        {
            var user = new User
            {
                Username = username
            };

            return IsBannedUser(user);
        }

        private static string EncryptPassword(string password)
        {
            var sha1 = new SHA1CryptoServiceProvider();
            var utf8 = new UTF8Encoding();
            var bytes = sha1.ComputeHash(utf8.GetBytes(password));
            return BitConverter.ToString(bytes).Replace("-", "");
        }

        /// <summary>
        ///     Remove a user
        /// </summary>
        /// <param name="username">Represent's the user's username</param>
        public static void RemoveUser(string username)
        {
            UserDAL.RemoveUser(username);
        }

        /// <summary>
        ///     Change user's status (Ban or unban user)
        /// </summary>
        /// <param name="username">Represent's the user's username</param>
        /// <param name="status">Represent's the user's status (Active or Disabled)</param>
        public static void ChangeUserStatus(string username, bool status)
        {
            var user = new User
            {
                Username = username,
                Status = status
            };

            UserDAL.ChangeUserStatus(user);
        }

        /// <summary>
        ///     Get user by username
        /// </summary>
        /// <param name="username">Represent's the user's username</param>
        /// <returns>User</returns>
        public static User GetUserByUsername(string username)
        {
            return UserDAL.GetUserByUsername(username);
        }

        /// <summary>
        ///     Request reset password
        /// </summary>
        /// <param name="username">Represent's the user's username</param>
        /// <returns>Unique id</returns>
        public static Guid RequestResetPassword(string username)
        {
            return UserDAL.RequestResetPassword(username);
        }

        /// <summary>
        ///     Request reset password
        /// </summary>
        /// <param name="uniqueId">Represent's the unique id</param>
        /// <returns>Valid or not</returns>
        public static bool IsResetLinkValid(Guid uniqueId)
        {
            return UserDAL.IsResetLinkValid(uniqueId);
        }

        /// <summary>
        ///     Change password
        /// </summary>
        /// <param name="uniqueId">Represent's the unique id</param>
        /// <param name="password">Represent's the encrypted password</param>
        public static void UpdatePassword(Guid uniqueId, string password)
        {
            UserDAL.UpdatePassword(uniqueId, EncryptPassword(password));
        }

        /// <summary>
        ///     Get reset password requestor's username
        /// </summary>
        /// <param name="uniqueId">Represent's the unique id</param>
        /// <returns>User's username</returns>
        public static string GetResetPasswordRequestorUsername(Guid uniqueId)
        {
            return UserDAL.GetResetPasswordRequestorUsername(uniqueId);
        }

        /// <summary>
        ///     Send reset password link
        /// </summary>
        /// <param name="link">Represent's the confirmation link</param>
        /// <param name="uniqueId">Represent's the reset unique Id</param>
        /// <param name="name">Represent's the reset user's name</param>
        /// <param name="eMail">Represent's the reset user's email</param>
        public static void SendResetPasswordLink(string link, Guid uniqueId, string name, string eMail)
        {
            string body = "Dear " + name + "<br/><br/>" +
                          "Please click on the following link to reset your password:<br/>" +
                          "<a href='" + link + "'>" + link + "</a>" +
                          "<br><b>Note: This link is only valid for the next 24 hours</b>" +
                          "<br>If you did not requested for reset password, then please ignore this message." +
                          "<br/><br/>Sincerely," +
                          "<br/><b>Fast Track Cab Service online support team<b>";

            MailMessage message = new MailMessage("www.weconnect@gmail.com", eMail,
                "Reset password mail - FastTrack",
                body)
            {
                IsBodyHtml = true
            };

            new SmtpClient().Send(message);
        }
    }
}